package com.vitabuybuy.tv.ui.mine.bidding.viewmodel

import android.view.View
import androidx.databinding.ObservableArrayList
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.MyBiddingListDto
import com.vitabuybuy.tv.inter.OnItemBiddingClickListener
import com.vitabuybuy.tv.network.BiddingRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class MyBiddingViewModel : BaseViewModel() {

    val goodslist = ObservableArrayList<MyBiddingListDto>()

    //网格布局的adapter绑定
    val goodsBinding: OnItemBind<MyBiddingListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: MyBiddingListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_my_bidding).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    private val itemOnClickListener = object : OnItemBiddingClickListener {
        override fun onItemClick(item: MyBiddingListDto) {
            defUI.msgEvent.postValue(MessageModel(2, obj = item))
        }
    }

    fun getGoodsList(status: String, pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = BiddingRepository.getMyBiddingList(status, pageNumber).await()
                if (pageNumber == 1) {
                    goodslist.clear()
                }
                goodslist.addAll(it)
                if (goodslist.isEmpty()) {
                        statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }
}
